import {login, logout, getInfo, getZixunData, getZixunAddData, getZixunRemoteData} from '@/api/user'
import {getToken, setToken, removeToken, setRouterItem, removeRouterItem} from '@/utils/storage'
import router, {resetRouter} from '@/router';
import {md5} from '@/utils/validate';
import zixun from './zixun'

const state = {
  token: getToken(),
  name: 'admin',
  avatar: '',
  loginTime: "",
  // roles: [],
  user_id: '',
  router_id: [],
  cachedViews: [
    'news_create',
    'news_edit'
  ],
  users: {
    username: "yy",
    password: "123456", token: "abc"
  },
  newData: [
    {
      "bianhao": 987757,
      "listname": "英雄联盟兰博皮肤",
      "leixing": "皮肤",
      "oldprice": 147,
      "newprice": 100,
      "num": 5000,
      "time": "2019-10-18 17:39:45",
      "state": false,
      "name": "英雄联盟",
      "qufu": "北京区",
      "type": "自营",
      "and": "1"
    },
    {
      "bianhao": 987758,
      "listname": "英雄联盟兰博皮肤",
      "leixing": "皮肤",
      "oldprice": 147,
      "newprice": 100,
      "num": 5000,
      "time": "2019-10-18 17:39:45",
      "state": false,
      "name": "英雄联盟",
      "qufu": "北京区",
      "type": "自营",
      "and": "1"
    },
    {
      "bianhao": 987759,
      "listname": "英雄联盟兰博皮肤",
      "leixing": "皮肤",
      "oldprice": 147,
      "newprice": 100,
      "num": 5000,
      "time": "2019-10-18 17:39:45",
      "state": false,
      "name": "英雄联盟",
      "qufu": "北京区",
      "type": "自营",
      "and": "1"
    },
    {
      "bianhao": 987760,
      "listname": "英雄联盟兰博皮肤",
      "leixing": "皮肤",
      "oldprice": 147,
      "newprice": 100,
      "num": 5000,
      "time": "2019-10-18 17:39:45",
      "state": true,
      "name": "英雄联盟",
      "qufu": "北京区",
      "type": "自营",
      "and": "1"
    },
    {
      "bianhao": 987761,
      "listname": "英雄联盟兰博皮肤",
      "leixing": "皮肤",
      "oldprice": 147,
      "newprice": 100,
      "num": 5000,
      "time": "2019-10-18 17:39:45",
      "state": true,
      "name": "英雄联盟",
      "qufu": "北京区",
      "type": "自营",
      "and": "1"
    },
    {
      "bianhao": 987762,
      "listname": "英雄联盟兰博皮肤",
      "leixing": "皮肤",
      "oldprice": 147,
      "newprice": 100,
      "num": 5000,
      "time": "2019-10-18 17:39:45",
      "state": true,
      "name": "英雄联盟",
      "qufu": "北京区",
      "type": "自营",
      "and": "4"
    }
  ],
  list: [
    {
      "id": "200301",
      "name": "英雄联盟",
      "leixing": ["账号", "金币", "挑战书", "皮肤", "符文"],
      "qu": [
        {
          "name": "电信区",
          "fuwu": ["全服", "1", "2"]
        },
        {
          "name": "网通区",
          "fuwu": ["全服", "2", "3"]
        },
        {
          "name": "电信区",
          "fuwu": ["全服", "1", "2"]
        },
        {
          "name": "网通区",
          "fuwu": ["全服", "2", "3"]
        }
      ]
    },
    {
      "id": "200302",
      "name": "地下城与勇士",
      "leixing": ["账号", "金币", "挑战书", "皮肤", "符文"],
      "qu": [
        {
          "name": "电信区",
          "fuwu": ["全服", "1", "2"]
        },
        {
          "name": "网通区",
          "fuwu": ["全服", "2", "3"]
        },
        {
          "name": "电信区",
          "fuwu": ["全服", "1", "2"]
        },
        {
          "name": "网通区",
          "fuwu": ["全服", "2", "3"]
        }
      ]
    },

    {
      "id": "200303",
      "name": "绝地求生",
      "leixing": ["账号", "金币", "挑战书", "皮肤", "符文"],
      "qu": [
        {
          "name": "四川区",
          "fuwu": ["全服", "1", "2"]
        },
        {
          "name": "华北区",
          "fuwu": ["全服", "2", "3"]
        },
        {
          "name": "电信区",
          "fuwu": ["全服", "1", "2"]
        },
        {
          "name": "网通区",
          "fuwu": ["全服", "2", "3"]
        }
      ]
    },
    {
      "id": "200304",
      "name": "使命召唤",
      "leixing": ["账号", "金币", "挑战书", "皮肤", "符文"],
      "qu": [
        {
          " name": "1区",
          "fuwu": ["全服", "1", "2"]
        },
        {
          "name": "2区",
          "fuwu": ["全服", "2", "3"]
        },
        {
          " name": "3区",
          "fuwu": ["全服", "1", "2"]
        },
        {
          "name": "网通区",
          "fuwu": ["全服", "2", "3"]
        }
      ]
    },

    {
      "id": "200305",
      "name": "Epic",
      "leixing": ["账号", "金币", "挑战书", "皮肤", "符文"],
      "qu": [
        {
          " name": "4区",
          "fuwu": ["全服", "1", "2"]
        },
        {
          "name": "5区",
          "fuwu": ["全服", "2", "3"]
        },
        {
          " name": "6区",
          "fuwu": ["全服", "1", "2"]
        },
        {
          "name": "7区",
          "fuwu": ["全服", "2", "3"]
        }
      ]
    }
  ],
  zixun: zixun
};

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_LOGINTIME: (state, loginTime) => {
    state.loginTime = loginTime
  },
  // SET_ROLES: (state, roles) => {
  //   state.roles = roles
  // },
  SET_ROUTER_ID: (state, routerId) => {
    state.router_id = routerId;
  },
  SET_USER_ID: (state, userid) => {
    state.user_id = userid;
  },
  SET_LIST: (state, list) => {
    state.list = list;
    login(state, payload)
    {
      let {username, password} = payload
      if (state.users.username === username) {
        if (state.users.password === password) {
          state.name = username,
            localStorage.setItem("token", state.users.token)
        }
      }
    }
  }
}

const actions = {
  // user login
  login({commit}, userInfo) {
    // const {username, password} = userInfo;
    return new Promise((resolve, reject) => {
      login(userInfo).then(res => {

        let {avtor, loginTime, access_token, uid, username} = res.data;
        setToken(access_token);
        commit('SET_TOKEN', access_token);

        resolve();
      }).catch(error => {
        reject(error)
      })
    })
  },


// getList({commit},list){

// get user info
  getInfo({commit, state}) {
    // get user info
    return new Promise((resolve, reject) => {
      getInfo().then(async res => {
        let {avtor, loginTime, username, uid} = res.data
        // commit('SET_ROLES', roles);
        commit('SET_NAME', username);
        commit('SET_USER_ID', uid);
        commit('SET_AVATAR', avtor);
        commit('SET_LOGINTIME', loginTime);
        resolve(res)
      }).catch(error => {
        reject(error)
      });
    })
  },


// remove token
  // user logout
  logout({commit, state, dispatch}) {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        commit('SET_TOKEN', '');
        commit('SET_ROUTER_ID', []);
        removeToken();
        resetRouter();
        removeRouterItem();
        resolve();
      }, 0)
    })
  },

  // remove token
  resetToken({commit}) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      // commit('SET_ROLES', [])
      removeToken()
      resolve()
    })
  },
  //咨讯获取数据接口
  getZixunData(data) {
    return new Promise((resolve, reject) => {
      getZixunData(data).then(res => {
        // let {  } = res.data;
        // setToken(access_token);
        console.log(res);
        resolve();
      }).catch(error => {
        reject(error)
      })
    })
  },
  //咨讯添加接口
  getZixunAddData(data) {
    return new Promise((resolve, reject) => {
      getZixunAddData(data).then(res => {
        // let {  } = res.data;
        // setToken(access_token);
        console.log(res);
        resolve();
      }).catch(error => {
        reject(error)
      })
    })
  },
  //咨讯删除接口
  getZixunRemoteData(data) {
    return new Promise((resolve, reject) => {
      getZixunRemoteData(data).then(res => {
        // let {  } = res.data;
        // setToken(access_token);
        console.log(res);
        resolve();
      }).catch(error => {
        reject(error)
      })
    })
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}


